package cn.zhangjie.org.controller;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.util.ExcelUtils;
import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.org.domain.Shop;
import cn.zhangjie.org.domain.ShopLog;
import cn.zhangjie.org.dto.ShopDto;
import cn.zhangjie.org.query.ShopQuery;
import cn.zhangjie.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class ShopController {
    @Autowired
    private IShopService shopService;

    //登录校验
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Shop> findall(){
        return shopService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条" )
    public Shop findone(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            shopService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updataoradd(@RequestBody Shop shop){

        try {
            if(shop.getId()==null){
                shopService.add(shop);
                return JsonResult.me();
            }else {
                shopService.update(shop);
                return JsonResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Shop> pagequery(@RequestBody ShopQuery shopQuery){
        try {
            return shopService.queryPage(shopQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult pactchdel(@RequestBody List<Long> ids){

        try {
            shopService.pactchdel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }

    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
//前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult pass(@RequestBody ShopLog log){
        try {
            shopService.pass(log);
            return JsonResult.me();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }


    /**
     * 接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
//前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult reject(@RequestBody ShopLog log){
        try {
            shopService.reject(log);
            return JsonResult.me();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("失败");
        }
    }
    //后台生成excel并下载
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtils.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        /*System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop:list) {
            System.out.println(shop);
        }*/
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        for (Shop shop : list) {
            shopService.update(shop);
        }
    }

    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }



}
